package com.zzr.demo.redis.controller;

import com.zzr.demo.redis.service.CounterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: xiaomi
 * @date: 2021/10/8
 * @description: 简易计数器
 */
@RestController
@RequestMapping("counter/single")
@Api(tags = "计数器：简易版，对单一元素进行计数")
@Slf4j
public class SimpleCounterController {

    @Autowired
    @Qualifier("SingleCounter")
    CounterService counterService;

    @GetMapping("/incr")
    @ApiOperation("计数器增1")
    public long incr() {
        long increase = counterService.increase();
        return increase;
    }

    @GetMapping("/{num}/incr")
    @ApiOperation("计数器增n")
    public long incr(int num) {
        long increase = counterService.increase(num);
        return increase;
    }

    @GetMapping("/decr")
    @ApiOperation("计数器减1")
    public long decr() {
        long decrease = counterService.decrease();
        return decrease;
    }

    @GetMapping("/{num}/decr")
    @ApiOperation("计数器减n")
    public long decr(int num) {
        long decrease = counterService.decrease(num);
        return decrease;
    }

    @GetMapping("/reset")
    @ApiOperation("重置")
    public boolean reset() {
        counterService.init(0);
        return true;
    }
}
